package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.AccountDao;
import benny_driving_ser.entity.DriConRecs;
import benny_driving_ser.entity.DriTopUpRec;
import benny_driving_ser.unionpay.action.Purchase;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.FormatUtil;

public class AccountDaoImpl implements AccountDao {

	private AccountDaoImpl() {
	}

	private static class AccountDaoImplHolder {
		private static AccountDaoImpl instance = new AccountDaoImpl();
	}

	public static AccountDaoImpl getInstance() {
		return AccountDaoImplHolder.instance;
	}

	public String driTopUp(int driid, String cost) {// 暂缓
		String sql_query = "SELECT id FROM t_user WHERE id=" + driid;
		Connection con = DBUtil.getInstance().openConnection();
		Statement s;
		try {
			s = con.createStatement();
			ResultSet rs = s.executeQuery(sql_query);
			if (rs.next()) {
				String res = Purchase.getInstance().pay(driid + "", cost);
				if (res != null)
					if (!res.equals("1"))
						return res;
				return "4";// 网银反馈空
			}
			return "2";// 账号不存在
		} catch (SQLException e) {
			e.printStackTrace();
			return "3";// sql异常
		} finally {
			DBUtil.getInstance().closeConnection(con);
		}
	}

	public String userTopUp(int userid, String cost) {
		// TODO Auto-generated method stub
		return null;
	}

	public String userUnionPay(int formid) {
		// TODO Auto-generated method stub
		return null;
	}

	public String userAccPay(int formid) {
		// TODO Auto-generated method stub
		return null;
	}

	public String userCodePay(int formid, String code) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 
	 */
	public void driSavePay(String qn, int driid, String value,
			String orderTime, String orderNum) {
		String sql_savePayRec = "INSERT INTO t_pay (driid,je,sd,xtsj,orderNum,balance,lxid) VALUES (?,?,?,?,?,?,1)";
		String sql_queryAcc = "SELECT ye FROM t_user WHERE id = " + driid;
		String sql_calAcc = "UPDATE t_user SET t_user.ye = ? WHERE t_user.id = ?";

		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			con.setAutoCommit(false);
			Statement s1 = con.createStatement();
			ResultSet rs1 = s1.executeQuery(sql_queryAcc);
			if (rs1.next()) {
				double acc = rs1.getDouble(1);
				double sourceBalance = acc;
				acc += (Double.parseDouble(value) / 100);
				PreparedStatement ps1 = con.prepareStatement(sql_calAcc);
				ps1.setDouble(1, acc);
				ps1.setInt(2, driid);
				ps1.executeUpdate();
				PreparedStatement ps2 = con.prepareStatement(sql_savePayRec);
				ps2.setInt(1, driid);
				ps2.setDouble(2, Double.parseDouble(value) / 100);
				ps2.setString(3, qn);
				ps2.setString(4, orderTime);
				ps2.setString(5, orderNum);
				ps2.setDouble(6, sourceBalance);
				ps2.executeUpdate();
				con.commit();
				DBUtil.getInstance().closeConnection(con);
				return;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return;
	}

	public String driAcc(String driid) {
		Connection con = DBUtil.getInstance().openConnection();
		JSONObject j = new JSONObject();
		try {
			String sql_query = "SELECT t_banksj.kh,t_user.ye FROM t_banksj RIGHT JOIN t_user ON t_user.id = t_banksj.hyid WHERE t_user.id = "
					+ Integer.parseInt(driid.trim());
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql_query);
			if (rs.next()) {
				j.put("bankcard", rs.getString(1) + "");
				j.put("balance", rs.getString(2) + "");
				return j.toString();
			}
			return "3";
		} catch (SQLException e) {
			e.printStackTrace();
			return "4";
		} catch (JSONException e) {
			e.printStackTrace();
			return "5";
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return "2";
		} finally {
			DBUtil.getInstance().closeConnection(con);
		}
	}

	public List<DriConRecs> getDriConRecs(int driid) {
		List<DriConRecs> dcrs = new ArrayList<DriConRecs>();
		String sql_query = "SELECT id,je,xtsj,project,formid FROM t_pay WHERE je<0 AND driid ="
				+ driid;
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql_query);
			while (rs.next()) {
				dcrs.add(new DriConRecs(rs.getInt(1), rs.getInt(5), rs
						.getInt(4), Math.abs(rs.getDouble(2)), rs.getString(3)));// time格式多了
			}
			DBUtil.getInstance().closeConnection(con);
			return dcrs;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return null;
	}

	public List<DriTopUpRec> getDriTopUpRecs(int driid) {
		String sql_query = "";

		return null;
	}

}
